package com.example.navigationdemo;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.os.Build;
import android.os.Bundle;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class MainFragment extends Fragment {

    private String CHANNEL_ID = "deepLink";
    private int notificationId = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        view.findViewById(R.id.btnToSecondFragment).setOnClickListener(v -> {
            //传统方式传递参数
//            Bundle bundle = new Bundle();
//            bundle.putString("name", "jack");
//            bundle.getInt("age", 18);

            //safeargs 传递参数
            Bundle bundle1 = new MainFragmentArgs.Builder()
                    .setName("tuyrt7")
                    .setAge(17)
                    .build().toBundle();

            Navigation.findNavController(view).navigate(R.id.main_to_second, bundle1);
        });

        view.findViewById(R.id.btnToSecondFragment2).setOnClickListener(Navigation.createNavigateOnClickListener(R.id.main_to_second));

        view.findViewById(R.id.btnSendNotification).setOnClickListener(v -> {
            sendNotification();
        });

        view.findViewById(R.id.btnToNavigation).setOnClickListener(
                Navigation.createNavigateOnClickListener(R.id.main_to_navigation)
                );

        view.findViewById(R.id.btnToLottie).setOnClickListener(
                Navigation.createNavigateOnClickListener(R.id.main_to_lottie)
        );
        return view;
    }

    private void sendNotification() {
        if (getActivity() == null) {
            return;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "channelName", importance);
            channel.setDescription("description");
            NotificationManager manager = getActivity().getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity(), CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("DeepLink Notification")
                .setContentText("Hello world,This is a content")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(getPendingIntent())
                .setAutoCancel(true);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
        notificationManager.notify(notificationId, builder.build());
    }

    private PendingIntent getPendingIntent() {
        if (getActivity() != null) {
            Bundle bundle = new Bundle();
            bundle.putString("params", "ParamsFromNotification_HelloMichael");
            return Navigation
                    .findNavController(getActivity(), R.id.btnSendNotification)
                    .createDeepLink()
                    .setGraph(R.navigation.nav_graph)
                    .setDestination(R.id.deepLinkFragment)
                    .setArguments(bundle)
                    .createPendingIntent();
        }
        return null;
    }
}